package com.xq.noteAliyun;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.xq.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @title: NoteUtil
 * @Author Tan
 * @Date: 2020/8/4 15:04
 * @Version 1.0
 * 验证码核心类
 */
@Component
public class NoteUtil {
    private final String AccessKeyID = "LTAI4FdEsLDDCvRBhp3B5Yac";
    private final String AccessKeySecret = "68JxETB2DnAUHXCSlsQhJQJscjlxt7";
    private final String TemplateCode = "SMS_198920253";
    private final String RegionId = "cn-hangzhou";

    @Autowired
    private RedisUtil redisUtils;

    /**
     * 返回验证码
     * @param phoneNumber
     * @return
     */
    public String SendSms(String phoneNumber) {
        //响应
        CommonResponse response=null;
        //随机6位数验证码
        String code = RandomCode.random();
        Map<String, String> map = new HashMap<>();
        map.put("code", code);
        //将map转为JSON格式 templateParam参数格式要求
        String templateParam = JSON.toJSON(map).toString();
        //获取redis验证码
        String redisCode = (String) redisUtils.get(phoneNumber);
        //判断redis中是否存在验证
        if (!StringUtils.isEmpty(redisCode)) {
            //用redis中的验证码覆盖随机产生的验证码
            code=redisCode;
            System.out.println("redis中存在" + redisCode);
            return code;
        } else {
            redisUtils.set(phoneNumber, code, 60);
            DefaultProfile profile = DefaultProfile.getProfile(RegionId, AccessKeyID, AccessKeySecret);
            IAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.setSysMethod(MethodType.POST);
            request.setSysDomain("dysmsapi.aliyuncs.com");
            request.setSysVersion("2017-05-25");
            request.setSysAction("SendSms");
            request.putQueryParameter("RegionId", RegionId);
            request.putQueryParameter("PhoneNumbers", phoneNumber);
            request.putQueryParameter("SignName", "新冠疫情管理系统");
            request.putQueryParameter("TemplateCode", TemplateCode);
            request.putQueryParameter("TemplateParam", templateParam);
            try {
                 response = client.getCommonResponse(request);
                System.out.println(response.getData());
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
            //将响应JSON数据转换为对象
            ResponseData responseData=JSON.parseObject(response.getData(),ResponseData.class);
            if(responseData.getCode().equals("OK")){
                return code;
            }else {
                return "报错了！"+responseData.getCode();
            }
        }
    }

}